package kr.hkit.password.fragment.item;

import java.util.ArrayList;

import kr.hkit.password.R;
import kr.hkit.password.dao.InfoDao;
import kr.hkit.password.db.MyDatabaseManager;
import android.app.Fragment;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class ItemView extends Fragment implements OnClickListener {
	private static final String TAG = "PW";
	public static final String ITEM_NAME = "itemName";

	private MyDatabaseManager mIdb;
	private TextView tvId;
	private TextView tvPw;
	private TextView tvUrl;
	private TextView tvName;
	private TextView tvAlarm;
	private TextView tvDate;
	private ArrayList<String> item;
	private Button btn;
	private Button btnUrl;
	private Fragment fragment;
	private Bundle args;
	private InfoDao info;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		Log.d(TAG, "ItemView onCreateView");
		View view = inflater.inflate(R.layout.item_view, container, false);
		info = (InfoDao) getActivity().getApplication();
		info.setMain(false);
		String name = null;
		Log.d(TAG, "ItemView getArguments()" + getArguments());
		if (getArguments() == null) {
			getFragmentManager().beginTransaction()
					.replace(R.id.content_frame, new ItemWebView()).commit();
		} else {
			name = getArguments().getString(ITEM_NAME);

			mIdb = MyDatabaseManager.getInstance(getActivity());
			Cursor c = mIdb.select("list", mIdb.itemColumns, "name=? and account=?",
					new String[] { name, info.getAccount() }, null, null, null);
			Log.d(TAG, "ItemView c.getCount() = " + c.getCount());
			tvId = (TextView) view.findViewById(R.id.id);
			tvPw = (TextView) view.findViewById(R.id.password);
			tvUrl = (TextView) view.findViewById(R.id.url);
			tvName = (TextView) view.findViewById(R.id.name);
			tvAlarm = (TextView) view.findViewById(R.id.tvAlarm);
			tvDate = (TextView) view.findViewById(R.id.tvDate);
			btn = (Button) view.findViewById(R.id.btnEdit);
			btnUrl = (Button) view.findViewById(R.id.btnUrl);
			btn.setOnClickListener(this);
			btnUrl.setOnClickListener(this);

			if (c.moveToNext()) {
				tvId.setText(c.getString(0));
				tvPw.setText(c.getString(1));
				tvUrl.setText(c.getString(2));
				tvName.setText(c.getString(3));
				tvDate.setText(c.getString(5));
				tvAlarm.setText(InfoDao.CYCLE[Integer.parseInt(c.getString(4))]);
				item = new ArrayList<String>();
				for (int i = 0; i < mIdb.itemColumns.length; i++){
					item.add(c.getString(i));
				}
			}
		}
		return view;
	}

	@Override
	public void onClick(View v) {
		args = new Bundle();
		switch (v.getId()) {
		case R.id.btnEdit:
			Log.d(TAG, "ItemViewFrag onClick()");
			args.putStringArrayList("item", item);
			fragment = new ItemUpdate();
			
			break;
		case R.id.btnUrl:
			Log.d(TAG, "ItemViewFrag Url onClick()");
			args.putString("url", item.get(2));
			
			ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
			ClipData cd = ClipData.newPlainText("url", item.get(1));
			cm.setPrimaryClip(cd);
			
			fragment = new ItemWebView();
			break;
		}
		fragment.setArguments(args);
		getFragmentManager().beginTransaction()
				.replace(R.id.content_frame, fragment).commit();
	}

}
